import { apiExistence } from 'js/api'
import { session } from 'util/storage'

export default {
  state: {
    userInfo: session.get('userInfo') || {}
  },
  getters: {
    token: state => state.userInfo.access_token,
    userName: state => state.userInfo.mobile,
    avatar: state => state.userInfo.userHeadImg,
    userRole: state => state.userInfo.levelName || '',
    userInfo: state => state.userInfo
  },
  actions: {
    // 判断用户是否存在
    fetchIsUserName({state}, payload) {
      apiExistence(payload.userName).then(data => {
        payload.callback && payload.callback(data)
      })
    }
  },
  mutations: {
    setUserInfo(state, payload) {
      session.set('userInfo', payload)
      state.userInfo = payload
    },
    clearUserInfo(state, payload) {
      session.remove('userInfo')
      state.userInfo = {}
    }
  }
}
